/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.shiep.aurora.calendar;

import java.awt.Color;
import java.awt.Graphics;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import work02.w5.model.DateWithLunar;
import work02.w5.model.ScheduleFrequence;
import work02.w5.model.ScheduleModel;

/**
 *
 * @author Eric
 */
public class DatePanel extends javax.swing.JPanel {

    private final boolean isDEBUG = true;
    private boolean isPressed = false;
    private boolean isEnter = false;
    private BackGround backGround;
    /**
     * Creates new form NewJPanel
     */
    public DatePanel(BackGround bg) {
        initComponents();
        setState(isDEBUG, isWeekday, isToday, hasSchedule, isCurrentShowMonth);
        this.backGround=bg;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (isEnter) {
            g.setColor(new Color(153, 153, 153, 100));
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        if (isPressed) {
            g.setColor(new Color(0, 200, 0, 100));
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lunarDayJLabel = new javax.swing.JLabel();
        sunDayJLabel = new javax.swing.JLabel();
        dayEventFlagJLabel = new javax.swing.JLabel();
        todayFlagJLabel = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(52, 43));
        setMinimumSize(new java.awt.Dimension(52, 43));
        setOpaque(false);
        setPreferredSize(new java.awt.Dimension(52, 43));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                formMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                formMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lunarDayJLabel.setFont(new java.awt.Font("微软雅黑", 1, 10)); // NOI18N
        lunarDayJLabel.setForeground(new java.awt.Color(51, 255, 51));
        lunarDayJLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lunarDayJLabel.setText("二十");
        lunarDayJLabel.setToolTipText("");
        lunarDayJLabel.setMaximumSize(new java.awt.Dimension(40, 13));
        lunarDayJLabel.setMinimumSize(new java.awt.Dimension(40, 13));
        lunarDayJLabel.setPreferredSize(new java.awt.Dimension(40, 13));
        add(lunarDayJLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 30, -1));

        sunDayJLabel.setFont(new java.awt.Font("微软雅黑", 0, 20)); // NOI18N
        sunDayJLabel.setForeground(new java.awt.Color(153, 153, 153));
        sunDayJLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        sunDayJLabel.setText("23");
        sunDayJLabel.setToolTipText("");
        sunDayJLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        add(sunDayJLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 30, 20));

        dayEventFlagJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/event.png"))); // NOI18N
        add(dayEventFlagJLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 0, -1, -1));

        todayFlagJLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/shiep/aurora/calendar/resources/todayFlag.png"))); // NOI18N
        add(todayFlagJLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 40));
    }// </editor-fold>//GEN-END:initComponents
    SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd日");

    /**
     * 单击日期元素
     *
     * @param evt
     */
    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        CalendarFrameAdapter cal = backGround.frameControl;
        cal.setCalendarSchedule( cal.schedule.getSchedule(getDate()));
        if (evt.getClickCount() == 2) {
            UIManager.scheduleP.setVisible(true);
            BackGround back = UIManager.background;
            Date startDate;
            try {
                startDate = chineseDateFormat.parse(back.frameControl.getShowedTime() + sunDayJLabel.getText() + "日");
            } catch (ParseException ex) {
                startDate = new Date();
            }
            UIManager.scheduleP.setContent(new ScheduleModel(startDate, startDate, "", "", 0, ScheduleFrequence.OnlyOnce),SchedulePanel.State.New);
            UIManager.scheduleP.setVisible(true);
        }
	}//GEN-LAST:event_formMouseClicked

    private void formMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseEntered
        isEnter = true;
        repaint();
    }//GEN-LAST:event_formMouseEntered

    private void formMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseExited
        isEnter = false;
        repaint();
    }//GEN-LAST:event_formMouseExited

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        isPressed = true;
        repaint();
    }//GEN-LAST:event_formMousePressed

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        isPressed = false;
        repaint();
    }//GEN-LAST:event_formMouseReleased
    /**
     * 是否隐藏
     */
    private boolean isShow = false;
    /**
     * 是否是工作日
     */
    private boolean isWeekday = true;
    /**
     * 是否是今天
     */
    private boolean isToday = false;
    /**
     * 是否是有预定的日子
     */
    private boolean hasSchedule = false;
    /**
     * 是否是本页显示的月份，就是是不是不应该显示的日子
     */
    private boolean isCurrentShowMonth = true;

    public void setState(boolean isShow, boolean isWeekday, boolean isToday, boolean hasSchedule, boolean isCurrentShowMonth) {
        this.isShow = isShow;
        this.isWeekday = isWeekday;
        this.isToday = isToday;
        this.hasSchedule = hasSchedule;
        this.isCurrentShowMonth = isCurrentShowMonth;

        this.setVisible(isShow);
        if (isWeekday) {
            sunDayJLabel.setForeground(new java.awt.Color(255, 255, 255));
            lunarDayJLabel.setForeground(new java.awt.Color(255, 255, 255));
        } else {
            sunDayJLabel.setForeground(new java.awt.Color(51, 255, 51));
            lunarDayJLabel.setForeground(new java.awt.Color(51, 255, 51));
        }
        todayFlagJLabel.setVisible(isToday);
        dayEventFlagJLabel.setVisible(hasSchedule);
        if (!isCurrentShowMonth) {
            sunDayJLabel.setForeground(new java.awt.Color(153, 153, 153));
            lunarDayJLabel.setForeground(new java.awt.Color(153, 153, 153));
        }
    }

    private void setShowedDate(String lunarDate, String sunDate) {
        this.lunarDayJLabel.setText(lunarDate);
        this.sunDayJLabel.setText(sunDate);
    }
    public void setDate(DateWithLunar date){
        this.date=date;
        setShowedDate(date.getLunarDay(), date.getTimeOfDay() + "");
    }
    public DateWithLunar getDate(){
        return date;
    }
    DateWithLunar date;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JLabel dayEventFlagJLabel;
    javax.swing.JLabel lunarDayJLabel;
    javax.swing.JLabel sunDayJLabel;
    javax.swing.JLabel todayFlagJLabel;
    // End of variables declaration//GEN-END:variables
}
